﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace yq.Language
{
    /// <summary>
    /// 相关配置
    /// </summary>
    public class Config
    {

        internal static object LockObj = new object();

        internal static string DbProviderName = "";

        internal static string DbConnectionString = "";

        public static ConfigEnum Default = ConfigEnum.Zh_CN;

        internal static bool Write = false;

        public static bool IsEnable = false;

        /// <summary>
        /// 分隔符
        /// </summary>
        internal const string Excision = "%--%";

        /// <summary>
        /// 配置文件路径
        /// </summary>
        private const string _DefaultFilePath = "Language.config";

        /// <summary>
        /// 站点配置文件路径
        /// </summary>
        private static string ConfigFilePath
        {
            get
            {
                return cn.Util.WebConfig.ConfigFilePath + _DefaultFilePath;;
            }
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        static Config()
        {
            if (System.IO.File.Exists(ConfigFilePath))
            {
                var root = XElement.Load(ConfigFilePath, LoadOptions.None);
                var allElement = from obj in root.Elements()
                                 select obj;

                foreach (XElement host in allElement)
                {
                    if (host.Name.LocalName == "connection")
                    {
                        DbConnectionString = GetXElementValue(host);
                        IsEnable = true;
                    }
                    else if (host.Name.LocalName == "providerName")
                    {
                        DbProviderName = GetXElementValue(host);
                    }
                    else if (host.Name.LocalName == "default")
                    {
                        switch (GetXElementValue(host))
                        {
                            case "Zh_CN": Default = ConfigEnum.Zh_CN; break;
                            case "Zh_TW": Default = ConfigEnum.Zh_TW; break;
                            case "EN": Default = ConfigEnum.EN; break;
                        }
                    }
                    else if (host.Name.LocalName == "write")
                    {
                        Write = GetXElementValue(host)=="true";
                    }
                }

                if (allElement.Count()!=0)
                {
                    IsEnable = true;
                }
            }
        }

        /// <summary>
        /// 取value
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        private static string GetXElementValue(XElement host)
        {
            return host.Attribute(XName.Get("value")) == null ? "" : host.Attribute(XName.Get("value")).Value;
        }

    }
}
